package com.brewdroid;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;

import com.brewdroid.db.DatabaseHelper;
import com.brewdroid.db.dao.AdjunctDAO;
import com.brewdroid.db.dao.HopDAO;
import com.brewdroid.db.dao.MaltDAO;
import com.brewdroid.db.dao.YeastDAO;
import com.brewdroid.model.Adjunct;
import com.brewdroid.model.Hop;
import com.brewdroid.model.Ingredient;
import com.brewdroid.model.IngredientStep;
import com.brewdroid.model.Malt;
import com.brewdroid.model.Yeast;

public class RecipeItemRow extends TableRow implements OnItemSelectedListener {
	
	public Button deleteButton;
	public Spinner ingredientTypeSpinner;
	public Spinner ingredientSpinner;
	public Spinner units;
	public EditText amount;
	private Context con;
	private DatabaseHelper dbHelper =null;
	private boolean initialLoad = true;
	private ArrayAdapter<String> adapter;
	
	public static final String MALT = "Malt";
	public static final String HOP = "Hop";
	public static final String YEAST = "Yeast";
	public static final String ADJUNCT = "Adjunct";

	public RecipeItemRow(Context context, IngredientStep i, BrewDroidActivity b, DatabaseHelper db) {
		super(context);
		con = context;
		this.dbHelper=db;
	
		deleteButton = new Button(context);
		deleteButton.setText("X");
		deleteButton.setOnClickListener(b);
		
		ingredientTypeSpinner = new Spinner(context);
		ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, getIngredientTypeList());
		typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ingredientTypeSpinner.setAdapter(typeAdapter);
		ingredientTypeSpinner.setOnItemSelectedListener(this);
		ingredientTypeSpinner.setPrompt("Select Type");
		
		ingredientSpinner = new Spinner(context);
		adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, getIngredientList(i));
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ingredientSpinner.setAdapter(adapter);
		ingredientSpinner.setPrompt("Select Ingredient");
    	
    	units = new Spinner(context);
    	ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, getUnitsList());
    	adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	units.setAdapter(adapter2);
		
    	final LayoutParams lParams = new LayoutParams(75,70);
    	amount = new EditText(context);
    	amount.setLayoutParams(lParams);
    	amount.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
    	
    	if(i != null){
    		setValues(i);
    	}
    	else{
    		String initialType = getIngredientTypeList().get(0);
    		setIngredientsList(initialType);
    	}
		
    	this.addView(deleteButton);
    	this.addView(ingredientTypeSpinner);
		this.addView(ingredientSpinner);
		this.addView(amount);
		this.addView(units);
	}
	
	public void setValues(IngredientStep i){
		Ingredient ing = i.getIngredient();
		int ingredientTypeSelection = 0;
		if(ing instanceof Hop){
			ingredientTypeSelection = getIngredientTypeList().indexOf(HOP);
		}
		else if(ing instanceof Malt){
			ingredientTypeSelection = getIngredientTypeList().indexOf(MALT);
		}
		else if(ing instanceof Yeast){
			ingredientTypeSelection = getIngredientTypeList().indexOf(YEAST);
		}
		else if(ing instanceof Adjunct){
			ingredientTypeSelection = getIngredientTypeList().indexOf(ADJUNCT);
		}
		else{
			throw new IllegalStateException("Ingredient does not inherit an acceptable child type");
		}
		
		ingredientTypeSpinner.setSelection(ingredientTypeSelection);
		setIngredientsList((String)(ingredientTypeSpinner.getSelectedItem()));
		
		//set the name of the selected ingredient to the ingredient name in the db
		ingredientSpinner.setSelection(adapter.getPosition(i.getIngredient().getName()));
		
		units.setSelection(getUnitsList().indexOf(i.getUnitType()));
		
		amount.setText(String.valueOf(i.getQuantity()));
		
	}
	
	public List<String> getIngredientTypeList(){
		List<String> list = new ArrayList<String>();
		list.add(MALT);
		list.add(HOP);
		list.add(YEAST);
		list.add(ADJUNCT);
		
		return list;
	}
	
	public List<String> getIngredientList(IngredientStep i){
		List<String> list = new ArrayList<String>();
		
		return list;
	}
	
	public List<String> getUnitsList(){
		List<String> list = new ArrayList<String>();
		list.add("oz");
		list.add("lbs");
		list.add("tsp");
		list.add("Tbsp");
		list.add("Pkg"); //package for yeast
		
		return list;
	}
	
	public void setIngredientsList(String type){

		if(type.equalsIgnoreCase(MALT)){
			adapter = new ArrayAdapter<String>(con, android.R.layout.simple_spinner_item, MaltDAO.getMaltStrings(dbHelper));
		}
		else if(type.equalsIgnoreCase(HOP)){
			adapter = new ArrayAdapter<String>(con, android.R.layout.simple_spinner_item, HopDAO.getHopStrings(dbHelper));
		}
		else if(type.equalsIgnoreCase(YEAST)){
			adapter = new ArrayAdapter<String>(con, android.R.layout.simple_spinner_item, YeastDAO.getYeastStrings(dbHelper));
		}
		else if(type.equalsIgnoreCase(ADJUNCT)){
			adapter = new ArrayAdapter<String>(con, android.R.layout.simple_spinner_item, AdjunctDAO.getAdjunctStrings(dbHelper));
		}
		else{
			throw new IllegalStateException("");
		}
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ingredientSpinner.setAdapter(adapter);
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
		if(!initialLoad){
			String value = (String)(ingredientTypeSpinner.getSelectedItem());
			setIngredientsList(value);
		}
		initialLoad = false;
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
